import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xingsu/page/user/change_pwd_page.dart';
import 'package:xingsu/page/user/viewmodel/user_manager_viewmodel.dart';
import 'package:xingsu/util/widget_util.dart';

class UserManagerPage extends StatelessWidget {
  final UserManagerViewModel _viewModel = UserManagerViewModel();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("用户管理"),
      ),
      body: ListView(
        padding: const EdgeInsets.symmetric(horizontal: 15),
        children: [
          InkWell(
            onTap: () {
              Get.to(() => ChangePwdPage());
            },
            child: Container(
              height: 50,
              alignment: Alignment.centerLeft,
              margin: const EdgeInsets.symmetric(horizontal: 15),
              decoration: BoxDecoration(
                  border: Border(
                      bottom: BorderSide(
                          color: Theme.of(context).dividerTheme.color!,
                          width: 0.8))),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: const [
                  Text(
                    "修改密码",
                  ),
                  Spacer(),
                  Icon(
                    Icons.arrow_right_outlined,
                    color: Colors.grey,
                  )
                ],
              ),
            ),
          ),
          const SizedBox(
            height: 20,
          ),
          WidgetUtil.btnWidget(title: "退出登录", clickCallback: _viewModel.logout),
          const SizedBox(
            height: 30,
          ),
          Center(
            child: InkWell(
              onTap: () {
                showCupertinoAlertDialog(context);
              },
              child: Container(
                padding: const EdgeInsets.symmetric(vertical: 5),
                child: const Text(
                  "账号注销",
                  style: TextStyle(color: Colors.red),
                ),
              ),
            ),
          )
        ],
      ),
    );
  }

  void showCupertinoAlertDialog(BuildContext context) {
    showDialog(
        context: context,
        builder: (BuildContext context) {
          return CupertinoAlertDialog(
            title: const Text("提示"),
            content: Column(
              children: const [
                Align(
                  alignment: Alignment(0, 0),
                  child: Text("确定要注销该账号？"),
                ),
              ],
            ),
            actions: <Widget>[
              CupertinoDialogAction(
                child: const Text("取消"),
                onPressed: () {
                  Get.back();
                },
              ),
              CupertinoDialogAction(
                child: const Text(
                  "确定",
                  style: TextStyle(color: Colors.red),
                ),
                onPressed: () {
                  Get.back();
                  _viewModel.logout();
                },
              ),
            ],
          );
        });
  }
}
